<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"llmysnow.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="必学必会: Gzip&amp; Keep Alive；HTTP资源缓存；Service Worker 实现渐进式应用；HTTP&#x2F;2加速传输；SSR加速渲染">
<meta property="og:type" content="article">
<meta property="og:title" content="优化6.传输加载优化">
<meta property="og:url" content="http://llmysnow.top/posts/66264067/index.html">
<meta property="og:site_name" content="奇幻的梦之旅">
<meta property="og:description" content="必学必会: Gzip&amp; Keep Alive；HTTP资源缓存；Service Worker 实现渐进式应用；HTTP&#x2F;2加速传输；SSR加速渲染">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111026005.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111030225.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111045369.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111541595.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111559334.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111611453.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111651788.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111646165.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131740043.jpg">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111655698.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131502415.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131519745.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131603933.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131632885.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203132103453.png">
<meta property="article:published_time" content="2022-04-23T11:27:04.408Z">
<meta property="article:modified_time" content="2022-04-23T11:27:47.133Z">
<meta property="article:author" content="LiLyn">
<meta property="article:tag" content="性能优化">
<meta property="article:tag" content="启用压缩 Gzip">
<meta property="article:tag" content="启用 Keep Alive">
<meta property="article:tag" content="HTTP 资源缓存">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111026005.png">

<link rel="canonical" href="http://llmysnow.top/posts/66264067/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>优化6.传输加载优化 | 奇幻的梦之旅</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="奇幻的梦之旅" type="application/atom+xml">
</head>
<!--即时预加载技术，在用户点击之前预先加载页面-->
<script src="https://cdn.jsdelivr.net/gh/TRHX/CDN-for-itrhx.com@2.0.2/js/instantclick-1.2.2.js" type="module"></script>
<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
	  <!--<a target="_blank" rel="noopener" href="https://github.com/llwodexue" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>-->
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">奇幻的梦之旅</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">http://llmysnow.top/</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://llmysnow.top/posts/66264067/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/logo.jpg">
      <meta itemprop="name" content="LiLyn">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇幻的梦之旅">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          优化6.传输加载优化
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-04-23 19:27:04" itemprop="dateCreated datePublished" datetime="2022-04-23T19:27:04+08:00">2022-04-23</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" itemprop="url" rel="index"><span itemprop="name">性能优化</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>
            <div class="post-description">必学必会: Gzip& Keep Alive；HTTP资源缓存；Service Worker 实现渐进式应用；HTTP/2加速传输；SSR加速渲染</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="启用压缩-Gzip"><a href="#启用压缩-Gzip" class="headerlink" title="启用压缩 Gzip"></a>启用压缩 Gzip</h2><p>Gzip 是用来做网络资源压缩，帮助我们减少资源文件在网络传输大小的技术，可以高达 90%</p>
<blockquote>
<p>如下是 MacOs 安装方法，Windows 安装方法及使用可以参考我这篇文章：<a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_38689395/article/details/121020211?spm=1001.2014.3001.5501">项目技术架构-Nginx 服务器搭建</a></p>
</blockquote>
<ul>
<li>安装 homebrew：<a target="_blank" rel="noopener" href="https://brew.sh/index_zh-cn">https://brew.sh/index_zh-cn</a></li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/bin/bash -c <span class="string">&quot;<span class="subst">$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)</span>&quot;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>安装 nginx</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">brew install nginx</span><br></pre></td></tr></table></figure>

<ul>
<li>运行 ngix</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo brew services start nginx</span><br></pre></td></tr></table></figure>

<ul>
<li>查看配置文件</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /usr/<span class="built_in">local</span>/etc/nginx/nginx.conf</span><br></pre></td></tr></table></figure>

<p>修改为如下配置：</p>
<ul>
<li>访问：<code>localhost:8090</code> 即可（注意每行结尾都需要加  <code>;</code>，路径需要使用 <code>/</code>）</li>
</ul>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">server</span><br><span class="line">&#123; </span><br><span class="line">  <span class="attribute">charset</span> utf-<span class="number">8</span>;                                        <span class="comment"># 字符编码</span></span><br><span class="line">  <span class="attribute">listen</span> <span class="number">8090</span>;                                          <span class="comment"># 端口</span></span><br><span class="line">  <span class="attribute">server_name</span> localhost;</span><br><span class="line">  <span class="attribute">root</span> E:/dist;                                         <span class="comment"># 资源文件路径</span></span><br><span class="line">  <span class="attribute">location</span> / &#123; 											<span class="comment"># 对所有路由生效的配置</span></span><br><span class="line">    <span class="attribute">add_header</span> Access-Control-Allow-Origin *;			<span class="comment"># 防止跨域（生产环境需改为实际域名）</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>配置 gzip：</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">http</span> &#123;</span><br><span class="line">  // 开启gzip</span><br><span class="line">  <span class="attribute">gzip</span> <span class="literal">on</span>;</span><br><span class="line">  // 文件至少1k才进行压缩</span><br><span class="line">  <span class="attribute">gzip_min_length</span> <span class="number">1k</span>;</span><br><span class="line">  // 压缩级别，有1-9，压缩比例越高，对cpu的消耗也越高，权衡下取6，比较合适的值</span><br><span class="line">  <span class="attribute">gzip_comp_level</span> <span class="number">6</span>;</span><br><span class="line">  // 压缩文件类型，通常会对文本类文件进行压缩，图片类一般不进行压缩</span><br><span class="line">  <span class="attribute">gzip_types</span> text/plain application/javascript application/x-javascript text/css application/xml text/xml text/javascript application/json;</span><br><span class="line">  // 对gzip已经压缩的静态资源直接利用</span><br><span class="line">  <span class="attribute">gzip_static</span> <span class="literal">on</span>;</span><br><span class="line">  // 会在响应头部添加vary的属性，告诉客户端我们是否启用了gzip压缩</span><br><span class="line">  <span class="attribute">gzip_vary</span> <span class="literal">on</span>;</span><br><span class="line">  // buffer优化压缩过程</span><br><span class="line">  <span class="attribute">gzip_buffers</span> <span class="number">4</span> <span class="number">16k</span>;</span><br><span class="line">  // 压缩使用的http版本</span><br><span class="line">  <span class="attribute">gzip_http_version</span> <span class="number">1</span>.<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="启用-Keep-Alive"><a href="#启用-Keep-Alive" class="headerlink" title="启用 Keep Alive"></a>启用 Keep Alive</h2><p>这个技术可以帮助我们对 TCP 链接进行复用，也就是说当我们和一台服务器进行 TCP 建立连接之后，接下来的请求就都不需要重复建立链接。Nginx 默认开启 <code>keep-alive</code></p>
<ul>
<li>它是 HTTP 标准中的一部分，多数情况是有益无害的，所以在 HTTP1.1 以后，Keep Alive 默认开启</li>
<li>Initial connection 为 TCP 链接的建立，后续资源加载就没有 Initial connection</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111026005.png"></p>
<p>可以在 Request Headers 中看到 <code>keep-alive</code> 参数</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111030225.png"></p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">http</span> &#123;</span><br><span class="line">  keepalive_timeout  65; // 超时时间，65s内没使用TCP链接就会断掉</span><br><span class="line">  keepalive_requests 100; // 客户端和服务端进行TCP链接后，会开始计数，第101个请求就需要重新建立 TCO链接</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="HTTP-资源缓存"><a href="#HTTP-资源缓存" class="headerlink" title="HTTP 资源缓存"></a>HTTP 资源缓存</h2><h3 id="缓存资源"><a href="#缓存资源" class="headerlink" title="缓存资源"></a>缓存资源</h3><ul>
<li>提高重复访问时资加载的速度</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111045369.png"></p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/kevingrace/p/10459429.html">Nginx下关于缓存控制字段cache-control的配置说明 - 运维小结</a></p>
</blockquote>
<p>HTTP 缓存方案：</p>
<ul>
<li>Cache-Control/Expires</li>
<li>Last-Modified + If-Modified-Since</li>
<li>Etag + If-None-Match</li>
</ul>
<p><strong>Cache-Control/Expires</strong></p>
<ul>
<li><p>HTTP 1.0 中通过 Pragma 控制页面缓存，通常设置为 <code>no-cache</code> 并加上 <code>expires: 0</code>（立即过期，下次再用时去服务端拿）</p>
</li>
<li><p>HTTP 1.1 中启用 Cache-Control 来控制页面是否缓存，常用参数：<code>no-cache</code>、<code>public</code>、<code>no-store</code>、<code>must-revalidate</code></p>
<p>配置后两个主要是为了兼容性问题</p>
</li>
<li><p>因为 JS 和 CSS 在 Webpack 里都使用 Hash 命名放，这也可以保证 HTML 更新到最新，拿到的 JS 和 CSS 也是最新的</p>
</li>
</ul>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">server</span><br><span class="line">&#123;</span><br><span class="line">  <span class="attribute">location</span> / &#123;</span><br><span class="line">    <span class="attribute">index</span> index.html index.htm;</span><br><span class="line">    <span class="attribute">try_files</span> $uri /index.html;</span><br><span class="line">    <span class="attribute">if</span> ($request_filename <span class="regexp">~* .*\.(?:htm|html)$)</span> &#123;</span><br><span class="line">      <span class="attribute">add_header</span> Cache-Control <span class="string">&quot;no-cache, must-revalidate&quot;</span>;</span><br><span class="line">	  <span class="attribute">add_header</span> <span class="string">&quot;Pragma&quot;</span> <span class="string">&quot;no-cache&quot;</span>;</span><br><span class="line">	  <span class="attribute">add_header</span> <span class="string">&quot;Expires&quot;</span> <span class="string">&quot;0&quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="attribute">if</span> ($request_filename <span class="regexp">~* .*\.(?:js|css)$)</span> &#123;</span><br><span class="line">      <span class="attribute">expires</span> <span class="number">7d</span>;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="attribute">if</span> ($request_filename <span class="regexp">~* .*\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)</span> &#123;</span><br><span class="line">      <span class="attribute">expires</span> <span class="number">7d</span>;</span><br><span class="line">	&#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>客户端第一次请求一个 URL，服务器返回状态是 200，同时有一个 <code>Last-Modified</code> 报头的属性标记</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Last-Modified:Tue, 24 Feb 2019 08:01:04 GMT</span><br></pre></td></tr></table></figure>

<p>客户端第二次请求此 URL，浏览器会向服务器传送 <code>If-Modified-Since</code> 报头，询问该时间是否被修改过。如果服务器资源没有变化，自动返回 304，内容为空，客户端直接从缓存中取内容即可；如果资源有变化，则向客户端发送最新资源</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">If-Modified-Since:Tue, 24 Feb 2019 08:01:04 GMT</span><br></pre></td></tr></table></figure>

<p>Etag 同理，第一次请求会服务器会返回 <code>Etag</code> 报头</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Etag:“5d8c72a5edda8d6a:3239“</span><br></pre></td></tr></table></figure>

<p>第二次请求会向服务器传送 <code>If-None-Match</code> 报头</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">If-None-Match:“5d8c72a5edda8d6a:3239“</span><br></pre></td></tr></table></figure>

<h3 id="缓存网站参考"><a href="#缓存网站参考" class="headerlink" title="缓存网站参考"></a>缓存网站参考</h3><blockquote>
<p>更多配置可以看：<a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers">HTTP Headers</a></p>
</blockquote>
<p><a target="_blank" rel="noopener" href="https://www.tmall.com/">天猫</a></p>
<ul>
<li><p><code>max-age</code>：设置缓存存储的周达周期，单位秒</p>
</li>
<li><p><code>s-maxage</code>：只用于共享缓存，比如：CDN 缓存（s -&gt; share）</p>
<p><code>max-age</code> 用于普通缓存，<code>s-maxage</code> 用于代理缓存</p>
</li>
<li><p>它会跟服务器进行重新确认（携带 <code>if-none-match</code> ）去确认</p>
</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111541595.png"></p>
<p><a target="_blank" rel="noopener" href="https://www.zhihu.com/signin">知乎</a></p>
<ul>
<li><code>public</code>：响应可以被任何对象（发送的客户端、代理服务器）缓存</li>
<li><code>private</code>：响应只能被单个用户缓存，不能作为共享缓存（代理服务器不能缓存）</li>
<li><code>no-store</code>：绝对禁止缓存</li>
<li><code>no-cache</code>：资源不进行缓存，但是设置了这个不代表浏览器不缓存，而是缓存前要向服务器确认资源是否被更改，因为有时为了保险起见还会加上 <code>private</code> 指令或将过期时间设为过去的时间</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111559334.png"></p>
<p><a target="_blank" rel="noopener" href="https://developers.google.com/">Google 开发者</a></p>
<ul>
<li><code>must-revalidate</code>：缓存必须在使用之前验证旧资源的状态，并且不可使用过期资源</li>
<li>由于通过了 service worker，它并没有真正和服务器进行确认，可以直接去使用</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111611453.png"></p>
<h2 id="Service-Worker"><a href="#Service-Worker" class="headerlink" title="Service Worker"></a>Service Worker</h2><blockquote>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/Service_Worker_API">Service Worker API</a></p>
</blockquote>
<ul>
<li><p>加速重复访问</p>
</li>
<li><p>离线支持</p>
<p>用户在没有网络的情况下（offline）也可以让用户访问我们的网页</p>
</li>
</ul>
<p>serviceWorker 也有自己的生命周期，首先要注册安装激活才能使用，打包后的目录里会生成 <code>asset-manifest.json</code> 里面定义了哪些资源要进行缓存、缓存文件的文件名、相关的版本信息会存在 <code>precache-manifest</code> 里，每个文件都有先关版本信息</p>
<ul>
<li>需要使用两个插件生成 serviceWorker，一个叫 WorkboxWebpackPlugin，另一个叫 ManifestPlugin（生成 <code>asset-manifest.json</code>）它会决定哪些资源进行缓存，通常会把所有静态资源 HTML、CSS、JS 都进行缓存，图片或视频资源一般不会缓存</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> SpeedMeasurePlugin = <span class="built_in">require</span>(<span class="string">&#x27;speed-measure-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> smp = <span class="keyword">new</span> SpeedMeasurePlugin()</span><br><span class="line"><span class="keyword">const</span> WorkboxWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;workbox-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> ManifestPlugin = <span class="built_in">require</span>(<span class="string">&#x27;webpack-manifest-plugin&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = smp.wrap(&#123;</span><br><span class="line">  plugins: [</span><br><span class="line">    <span class="keyword">new</span> WorkboxWebpackPlugin.GenerateSW(&#123;</span><br><span class="line">      clientsClaim: <span class="literal">true</span>,</span><br><span class="line">      exclude: [<span class="regexp">/\.map$/</span>, <span class="regexp">/asset-manifest\.json$/</span>],</span><br><span class="line">      importWorkboxFrom: <span class="string">&#x27;cdn&#x27;</span>,</span><br><span class="line">      navigateFallback: paths.publicUrlOrPath + <span class="string">&#x27;index.html&#x27;</span>,</span><br><span class="line">      navigateFallbackBlacklist: [</span><br><span class="line">        <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">&#x27;^/_&#x27;</span>),</span><br><span class="line">        <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">&#x27;/[^/?]+\\.[^/]+$&#x27;</span>),</span><br><span class="line">      ],</span><br><span class="line">    &#125;),</span><br><span class="line"></span><br><span class="line">    <span class="keyword">new</span> ManifestPlugin(&#123;</span><br><span class="line">      fileName: <span class="string">&#x27;asset-manifest.json&#x27;</span>,</span><br><span class="line">      publicPath: paths.publicUrlOrPath,</span><br><span class="line">      generate: <span class="function">(<span class="params">seed, files, entrypoints</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> manifestFiles = files.reduce(<span class="function">(<span class="params">manifest, file</span>) =&gt;</span> &#123;</span><br><span class="line">          manifest[file.name] = file.path</span><br><span class="line">          <span class="keyword">return</span> manifest</span><br><span class="line">        &#125;, seed)</span><br><span class="line">        <span class="comment">// 从入口文件开始把所有涉及到的文件全部加到asset-manifest.json里</span></span><br><span class="line">        <span class="comment">// app是入口文件，通常项目里的入口文件是main</span></span><br><span class="line">        <span class="keyword">const</span> entrypointFiles = entrypoints.app.filter(<span class="function"><span class="params">fileName</span> =&gt;</span> !fileName.endsWith(<span class="string">&#x27;.map&#x27;</span>))</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">          files: manifestFiles,</span><br><span class="line">          entrypoints: entrypointFiles,</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;),</span><br><span class="line">  ],</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>在入口文件里注册即可</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> serviceWorker <span class="keyword">from</span> <span class="string">&#x27;./serviceWorker&#x27;</span></span><br><span class="line"></span><br><span class="line">serviceWorker.register()</span><br></pre></td></tr></table></figure>

<p><strong>Service Worker 原理：</strong></p>
<ul>
<li>在客户端和服务端建立一个中间层，做了存储</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111651788.png"></p>
<p><strong>Service Worker 注意：</strong></p>
<ul>
<li><p>延长了首屏时间，但页面总加载时间减少</p>
</li>
<li><p>兼容性</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111646165.png"></p>
</li>
<li><p>只能在 localhost 或者 https 下使用（为了保证安全性）</p>
</li>
</ul>
<h2 id="HTTP2-提升"><a href="#HTTP2-提升" class="headerlink" title="HTTP2 提升"></a>HTTP2 提升</h2><blockquote>
<p><a target="_blank" rel="noopener" href="https://www.zhihu.com/question/306768582">HTTP 2.0 和 HTTP 1.1 相比有哪些优势呢？</a></p>
</blockquote>
<h3 id="HTTP2-优势"><a href="#HTTP2-优势" class="headerlink" title="HTTP2 优势"></a>HTTP2 优势</h3><ul>
<li><p>二进制传输</p>
<p>HTTP 1.1 基于文本传输，效率低且不安全</p>
<p>HTTP 2 基于二进制编码传输，安全且能进行很好的压缩，提高了传输效率</p>
</li>
<li><p>请求响应多路复用</p>
<p>HTTP 1.1 实现是基于请求-响应模型，同一个连接中 HTTP 完成一个事务才能处理下一个事务，如果响应迟迟不来，后续请求无法发送，造成了 <strong>对头阻塞</strong> 问题。如果并发多个请求就需要多个 TCP 连接，开启 keep-alive，虽然可以用多次，但是同一时刻只能有一个 HTTP 请求</p>
<p>HTTP 2 所有相同域名的请求都通过一个 TCP 连接并发完成，<strong>多个 Stream 复用一条 TCP 连接</strong></p>
</li>
<li><p>Server push</p>
<p>HTTP 1.1 不支持服务器主动推送资源给客户端，都是客户端向服务器发起请求后，才能获取到服务器响应的资源</p>
<p>HTTP 2 服务器可以主动推送资源文件，减少消息传递次数。客户端发起请求，必须使用奇数号 Stream，服务器主动推送，使用偶数号 Stream（会先发送 PUSH_PROMISE 帧，告诉客户端接下来在哪个 Stream 发送资源）</p>
</li>
<li><p>头部压缩（HTTP 协议报文是有 Header + Body 构成）</p>
<p>HTTP 1.1 可以使用头字段（Content-Encoding）指定 Body 压缩方式（gzip），但是 Header 没有针对它的优化手段</p>
<p>HTTP 2 使用 HPACK 算法进行压缩，对于常见的头通过 <strong>静态表和 Huffman 编码</strong> 方式，后续请求头，可以建立 <strong>动态表</strong></p>
</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131740043.jpg"></p>
<h3 id="开启-HTTP2"><a href="#开启-HTTP2" class="headerlink" title="开启 HTTP2"></a>开启 HTTP2</h3><ul>
<li>HTTPS</li>
<li>适合较高的请求量</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203111655698.png"></p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">server</span><br><span class="line">&#123; </span><br><span class="line">  <span class="attribute">listen</span> <span class="number">843</span> ssl;</span><br><span class="line">  <span class="attribute">server_name</span> localhost;</span><br><span class="line">  <span class="attribute">ssl</span> <span class="literal">on</span>;</span><br><span class="line">  <span class="attribute">ssl_certificate</span> /path/to/server.crt;</span><br><span class="line">  <span class="attribute">ssl_certificate_key</span> /path/to/server.key;</span><br><span class="line">  <span class="attribute">ssl_session_cache</span> shared:SSL:<span class="number">1m</span>;</span><br><span class="line">  <span class="attribute">ssl_session_timeout</span> <span class="number">5m</span>;</span><br><span class="line">  <span class="attribute">ssl_ciphers</span> HIGH:!aNULL:!MD5;</span><br><span class="line">  <span class="attribute">ssl_prefer_server_ciphers</span> <span class="literal">on</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>自签名证书</strong></p>
<ul>
<li>执行最得到 <code>server.crt</code> 和 <code>server.key</code>，在工程目录下新建 ssl 文件夹，将其拷贝进去</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">openssl genrsa -des3 -passout pass:x -out server.pass.key 2048</span><br><span class="line"></span><br><span class="line">openssl rsa -passin pass:x -<span class="keyword">in</span> server.pass.key -out server.key </span><br><span class="line"></span><br><span class="line">openssl req -new -key server.key -out server.csr </span><br><span class="line"></span><br><span class="line">openssl x509 -req -sha256 -days 3650 -<span class="keyword">in</span> server.csr -signkey server.key -out server.crt </span><br></pre></td></tr></table></figure>

<p>访问 <code>https://localhost:843</code>，会出现如下图。因为我们使用的是自签名证书，直接在键盘输入 <code>thisisunsafe</code>，页面就可以绕过证书的验证了</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131502415.png"></p>
<p>所有的网络资源都变成 http2 的协议了，还有 h3（这里 h3 是对 google 外部资源的请求）</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131519745.png"></p>
<p>HTTP 1.1 虽然可以用 <code>keep-alive</code> 复用同个 TCP 链接，但是资源还是有顺序的，会形成阻塞</p>
<p>HTTP 2 真正做到了异步或并发的对资源进行传输，同一个时刻可以发起多个资源请求，可以将不同资源信息同时通过网络传回浏览器</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131603933.png"></p>
<h3 id="Server-Push（服务器推送）"><a href="#Server-Push（服务器推送）" class="headerlink" title="Server Push（服务器推送）"></a>Server Push（服务器推送）</h3><p>正常客户端拿到资源都是向服务器发起请求，服务器再把资源推送给客户端，这个来回是有消耗的（TTFB），如果能让服务器提前把这些东西推送到客户端，就能节约一定的网络开销</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">server</span><br><span class="line">&#123;</span><br><span class="line">  <span class="attribute">location</span> / &#123;</span><br><span class="line">    <span class="attribute">index</span> index.html index.htm;</span><br><span class="line">    <span class="attribute">http2_push</span> /img/me0.jpg;</span><br><span class="line">    <span class="attribute">http2_push</span> /img/me1.jpg;</span><br><span class="line">    <span class="attribute">http2_push</span> /img/me2.jpg;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>重启 nginx，可以发现图片没有了绿色部分（TTFB），少了请求返还回路的过程</p>
<ul>
<li>Initiator 图片为 Push，这种资源是通过 server push 提前推送到浏览器的</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203131632885.png"></p>
<h2 id="服务端渲染-SSR"><a href="#服务端渲染-SSR" class="headerlink" title="服务端渲染 SSR"></a>服务端渲染 SSR</h2><blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/yacan8/blog/issues/30">彻底理解服务端渲染 - SSR原理 #30</a></p>
</blockquote>
<p><strong>SSR 好处：</strong></p>
<ul>
<li>加速首屏加载</li>
<li>更好的 SEO</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/jslearn-img/202203132103453.png"></p>
<p><strong>基于 Next.js 实现SSR</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm init -y</span><br><span class="line">npm install next react react-dom</span><br></pre></td></tr></table></figure>

<p>添加 scripts 执行脚本：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// package.json</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;dev&quot;</span>: <span class="string">&quot;next&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在 <code>index.jsx</code> 中添加如下内容，之后 <code>npm run dev</code> 即可</p>
<ul>
<li>服务端渲染会把页面上显示的所有内容都放在 html 里</li>
<li>next.js 已经帮我们把代码进行基于路由的代码拆分，里面提供了 Link 组件</li>
</ul>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Header <span class="keyword">from</span> <span class="string">&#x27;./Header.jsx&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; (</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;Header /&gt;</span><br><span class="line">    &lt;p&gt;Home&lt;/p&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p><strong>是否使用 SSR</strong></p>
<ul>
<li>架构-大型，动态页面，面向公众用户（是否关心首屏速度）</li>
<li>搜索引擎排名很重要（前面的页面使用静态页面，后面页面使用 vue react 实现动态加载）</li>
</ul>

    </div>

    
    
    

	<div>
		  
			
<div class="my_post_copyright clearfix">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article class="box-left">
    <img src="https://gitee.com/lilyn/pic/raw/master/logo.jpg">
  </article>
  <article class="box-right">
    <p><span>本文标题:</span><a href="/posts/66264067/">优化6.传输加载优化</a></p>
    <p><span>文章作者:</span><a href="/" title="访问 LiLyn 的个人博客">LiLyn</a></p>
    <p><span>原始链接:</span><a href="/posts/66264067/" title="优化6.传输加载优化">http://llmysnow.top/posts/66264067/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://llmysnow.top/posts/66264067/"  aria-label="复制成功！"></i></span>
  </p>
    <p><span>许可协议:</span>转载请保留原文链接及作者。</p>  
  </article>
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>

		  
	</div>

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" rel="tag"><i class="fa fa-tag"></i> 性能优化</a>
              <a href="/tags/%E5%90%AF%E7%94%A8%E5%8E%8B%E7%BC%A9-Gzip/" rel="tag"><i class="fa fa-tag"></i> 启用压缩 Gzip</a>
              <a href="/tags/%E5%90%AF%E7%94%A8-Keep-Alive/" rel="tag"><i class="fa fa-tag"></i> 启用 Keep Alive</a>
              <a href="/tags/HTTP-%E8%B5%84%E6%BA%90%E7%BC%93%E5%AD%98/" rel="tag"><i class="fa fa-tag"></i> HTTP 资源缓存</a>
          </div>
		 
		<!--post-tags 彩色标签-->
		<script type="text/javascript">
		var tagsall=document.getElementsByClassName("post-tags")
		for (var i = tagsall.length - 1; i >= 0; i--){
			var tags=tagsall[i].getElementsByTagName("a");
			for (var j = tags.length - 1; j >= 0; j--) {
				var r=Math.floor(Math.random()*75+130);
				var g=Math.floor(Math.random()*75+100);
				var b=Math.floor(Math.random()*75+80);
				tags[j].style.background = "rgb("+r+","+g+","+b+")";
				tags[j].style.color = "#fff";
				tags[j].style.padding = "0 10px";
			}
		}                        
		</script>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/e8da84b8/" rel="prev" title="优化5.构建优化">
      <i class="fa fa-chevron-left"></i> 优化5.构建优化
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/9e7b88f/" rel="next" title="优化7.前沿优化解决方案">
      优化7.前沿优化解决方案 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%AF%E7%94%A8%E5%8E%8B%E7%BC%A9-Gzip"><span class="nav-text">启用压缩 Gzip</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%AF%E7%94%A8-Keep-Alive"><span class="nav-text">启用 Keep Alive</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#HTTP-%E8%B5%84%E6%BA%90%E7%BC%93%E5%AD%98"><span class="nav-text">HTTP 资源缓存</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%93%E5%AD%98%E8%B5%84%E6%BA%90"><span class="nav-text">缓存资源</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%93%E5%AD%98%E7%BD%91%E7%AB%99%E5%8F%82%E8%80%83"><span class="nav-text">缓存网站参考</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Service-Worker"><span class="nav-text">Service Worker</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#HTTP2-%E6%8F%90%E5%8D%87"><span class="nav-text">HTTP2 提升</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#HTTP2-%E4%BC%98%E5%8A%BF"><span class="nav-text">HTTP2 优势</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%80%E5%90%AF-HTTP2"><span class="nav-text">开启 HTTP2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Server-Push%EF%BC%88%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%8E%A8%E9%80%81%EF%BC%89"><span class="nav-text">Server Push（服务器推送）</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B8%B2%E6%9F%93-SSR"><span class="nav-text">服务端渲染 SSR</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LiLyn"
      src="/images/logo.jpg">
  <p class="site-author-name" itemprop="name">LiLyn</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">162</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/llwodexue" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;llwodexue" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/qq_38689395" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38689395" rel="noopener" target="_blank"><i class="fas fa-copyright fa-fw"></i>CSDN</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://scx69.gitee.io/blogs/" title="https:&#x2F;&#x2F;scx69.gitee.io&#x2F;blogs&#x2F;" rel="noopener" target="_blank">花开满城</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yixin-oss.gitee.io/daytoy/" title="http:&#x2F;&#x2F;yixin-oss.gitee.io&#x2F;daytoy&#x2F;" rel="noopener" target="_blank">daytoy</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiLyn</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">463k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:01</span>
</div>

<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>-->

<!--
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>-->

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  <!-- 动态线条背景 -->
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  <!-- 页面点击小红心 -->
  <script type="text/javascript">
	!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
  </script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>